<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
             :close-on-press-escape='false' :fullscreen='true'>
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
             
        <el-form-item label="上架策略号" prop="strategykey">
            <el-input size="small" v-model="dataForm.strategykey" placeholder="上架策略号" disabled></el-input>
        </el-form-item>
        <el-form-item label="上架策略名称" prop="strategyname">
            <el-input size="small" v-model="dataForm.strategyname" placeholder="上架策略名称"></el-input>
        </el-form-item>
          <!-- <el-form-item label="创建人" prop="adduser">
            <el-input v-model="dataForm.adduser" placeholder="创建人"></el-input>
          </el-form-item>
          <el-form-item label="创建时间" prop="addtime">
            <el-input v-model="dataForm.addtime" placeholder="创建时间"></el-input>
          </el-form-item>
          <el-form-item label="更新人" prop="updatuser">
            <el-input v-model="dataForm.updatuser" placeholder="更新人"></el-input>
          </el-form-item>
          <el-form-item label="更新时间" prop="updatetime">
            <el-input v-model="dataForm.updatetime" placeholder="更新时间"></el-input>
          </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button size="small" @click="visible = false">取消</el-button>
      <el-button size="small" type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import {ref, reactive, onMounted, nextTick, computed} from 'vue';
import {getPutawaystrategyById,putPutawaystrategy,postPutawaystrategy} from "@/network/lswms/base/Putawaystrategy";
 import {getSequenceByType}  from  "@/network/lswms/base/Sequence.js"
import {ElMessage} from 'element-plus'
import _ from 'lodash'


export default {
  name: "putawaystrategyAddOrUpdate",
  emits: ["refreshDataList"],
  setup(props, {emit}) {
    const visible = ref(false);
    const dataForm = ref({
		          id: '',
          strategykey: '',
          strategyname: '',
          adduser: '',
          addtime: '',
          updatuser: '',
          updatetime: ''
    });


    const dataRule = reactive({   
                strategykey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          strategyname: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          /* adduser: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          addtime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          updatuser: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          updatetime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ]  */  })

    const dataFormRef = ref(null)

    const init = () => {
      visible.value = true;
      nextTick(function () {
        dataFormRef.value.resetFields();
        if (dataForm.value.id) {
		getputawaystrategyInfo();
		}else{
			strategykeyFn()
		}
        
      })
    }

	const getputawaystrategyInfo =()=>{
		getPutawaystrategyById(dataForm.value.id).then(function (res) {
		  if (res.code !== 0) {
		    return ElMessage.error(res.msg);
		  }
		  dataForm.value = _.assignIn({}, dataForm.value, res.data);
		}).catch((err) =>{
		});
	}

const strategykeyFn= ()=>{

                                getSequenceByType('putawaystrategy').then((res)=>{
                                        if (res.code !== 0) {
                                                return ElMessage.error(res.msg);
                                            }
                                            dataForm.value.strategykey =  res.data;
                                        }).catch((err) =>{
                                        });
                        }

    // 表单提交
    const dataFormSubmitHandle = _.debounce(() => {
      dataFormRef.value.validate(function (valid) {
        if (!valid) {
          return false;
        }
		
        let qing = ()=> {
          return !dataForm.value.id ?  postPutawaystrategy(dataForm.value) : putPutawaystrategy(dataForm.value)
        }
        qing().then(function (res) {
          if (res.code !== 0) {
            return ElMessage.error(res.msg);
          }
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 500,
            onClose: function () {
              visible.value = false
              emit('refreshDataList')
            }
          })
        }).catch(function () {
        });

      })
    }, 1000, {'leading': true, 'trailing': false})

    return {
      visible,
      dataForm,
      dataRule,
      init,
      dataFormSubmitHandle,
      dataFormRef
    }
  },


}
</script>

<style scoped>

</style>